/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package reduce.fz.com.reduce.util.http;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

import com.android.volley.Response;

import reduce.fz.com.reduce.util.base.ReduceApplication;

/**
 * A canned request for getting an image at a given URL and calling
 * back with a decoded Bitmap.
 */
public class ImageRequest extends com.android.volley.toolbox.ImageRequest implements ChangeDomainPolicy {

    private String mUrl;

    /**
     * 请求domain会根据网络情况自动切换
     */
    private static String request_domain = null;
    private static int change = 0 ;
    static{
        request_domain = "";
    }

    private final static String HTTP_PREFIX = "http://";
    private final static String HTTPS_PREFIX = "https://";
    /**
     * Creates a new image request, decoding to a maximum specified width and
     * height. If both width and height are zero, the image will be decoded to
     * its natural size. If one of the two is nonzero, that dimension will be
     * clamped and the other one will be set to preserve the image's aspect
     * ratio. If both width and height are nonzero, the image will be decoded to
     * be fit in the rectangle of dimensions width x height while keeping its
     * aspect ratio.
     *
     * @param url URL of the image
     * @param listener Listener to receive the decoded bitmap
     * @param maxWidth Maximum width to decode this bitmap to, or zero for none
     * @param maxHeight Maximum height to decode this bitmap to, or zero for
     *            none
     * @param decodeConfig Format to decode the bitmap to
     * @param errorListener Error listener, or null to ignore errors
     */
    public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
            Config decodeConfig, Response.ErrorListener errorListener) {
        super( url, listener , maxWidth , maxHeight , decodeConfig , errorListener);
        if(!(url.startsWith(HTTP_PREFIX) || url.startsWith(HTTPS_PREFIX))){//feel提供的非完整路径，需要拼写feel的img域名才可访问
            mUrl = request_domain + url;
        } else {//完整的图片路径，可以直接访问
            withHttpPrefix = true;
            mUrl = url;
        }
        setRetryPolicy(new FeelImageRetryPolicy(this));
    }

    /**
     * 是否带有http的图片路径，默认不带有
     */
    private boolean withHttpPrefix = false;
    /**
     * Returns the URL of this request.
     * 重写是为了实现domain 和 ip 切换
     */
    @Override
    public String getUrl() {
        return mUrl;
    }
    /**
     * 实现切换 domain 和 IP
     */
    @Override
    public void changeDomainIP(){
        if(withHttpPrefix){//如果是完整的图片路径不可更改domain
            return;
        }
        String old_domain = request_domain;
        change++;
        if( change % 3 == 0){
            request_domain = old_domain;
        } else if( change % 3 == 1 ) {
            request_domain = old_domain;
        } else {
            request_domain = old_domain;
        }
        mUrl = mUrl.replace(old_domain , request_domain);
    }

}
